package edu.wright.ceg4980.year2013.keyboard;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.Scanner;

public class ProcessKeyboardData {
	public static void main(String[] args) throws Exception {
		if (args.length == 0) {
			printUsage();
			System.exit(0);
		}
		
		String srcFile = args[0];
		String outFileName = args[1];
		
		KeySequenceData ksd = null;
		try {
			ksd = processInputFile(srcFile);
		} catch (FileNotFoundException ex) {
			System.out.printf("Error: Input file %s could no be found.\n", srcFile);
			System.exit(1);
		}
		
		// calculate key down to key down times
		long[][] keyDownData = new long[ksd.keyDownTimes.length][0];
		for (int i = 0; i < ksd.keyDownTimes.length; ++i) {
			long[] currentKeyData = ksd.keyDownTimes[i];
			keyDownData[i] = new long[currentKeyData.length];
			keyDownData[i][0] = 0;
			for (int j = 1; j < currentKeyData.length; ++j) {
				keyDownData[i][j] = currentKeyData[j] - currentKeyData[j - 1];
			}
		}
		
		// calculate key press times
		long[][] keyPressData = new long[ksd.keyDownTimes.length][0];
		for (int i = 0; i < ksd.keyDownTimes.length; ++i) {
			long[] currentKeyDownData = ksd.keyDownTimes[i];
			long[] currentKeyUpData = ksd.keyUpTimes[i];
			keyPressData[i] = new long[currentKeyDownData.length];
			for (int j = 0; j < currentKeyDownData.length; ++j) {
				keyPressData[i][j] = currentKeyUpData[j] - currentKeyDownData[j];
			}
		}

		BufferedWriter bw = new BufferedWriter(new FileWriter(outFileName));
		bw.write("@RELATION keyboard"); bw.newLine();
		bw.newLine();
		for (int i = 0; i < ksd.keyDownTimes.length; ++i) {
			bw.write(String.format("@ATTRIBUTE keyDownToKeyDown%d  NUMERIC", i)); bw.newLine();
			bw.write(String.format("@ATTRIBUTE keyPress%d  NUMERIC", i)); bw.newLine();
		}
		bw.write("@ATTRIBUTE class  {username}"); bw.newLine();
		bw.newLine();
		
		bw.write("@DATA");bw.newLine();
		for (int j = 0; j < keyDownData[0].length; ++j) {
			for (int i = 0; i < ksd.keyDownTimes.length; ++i) {
				bw.write(String.format("%d,%d,", keyDownData[i][j], keyPressData[i][j]));
			}
			bw.write("username");
			bw.newLine();
		}
		
		bw.close();
		
		System.out.println("Done.");
	}

	public static void printUsage() {
		System.out.println("Usage:\n" +
				"\tProcessKeyboardData src out [-rel] [-kd] [-dur]");
	}
	
	private static KeySequenceData processInputFile(String inFileName) throws FileNotFoundException {
		KeySequenceData ksd = new KeySequenceData();
		
		Scanner s = new Scanner(new File(inFileName));
		// discard first line containing password
		s.nextLine();
		
		int numAttempts = s.nextInt();
		ksd.keyDownTimes = new long[numAttempts][0];
		ksd.keyUpTimes = new long[numAttempts][0];
		
		for (int i = 0; i < numAttempts; ++i) {
			int numKeyDowns = s.nextInt();
			int numKeyUps = s.nextInt();

			ksd.keyDownTimes[i] = new long[numKeyDowns];
			for (int j = 0; j < numKeyDowns; ++j) {
				s.nextInt();
				ksd.keyDownTimes[i][j] = s.nextLong();
			}
			
			ksd.keyUpTimes[i] = new long[numKeyUps];
			for (int j = 0; j < numKeyUps; ++j) {
				s.nextInt();
				ksd.keyUpTimes[i][j] = s.nextLong();
			}
		}
		
		return ksd;
	}

	private static class KeySequenceData {
		public long[][] keyDownTimes;
		public long[][] keyUpTimes;
	}
}
